package com.bagus.mediapipefacedetection.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

public class CommonUtil {
    /***
     * 找没有前置摄像头的测试
     */
    public static boolean checkDeviceSupport(Context context) {
        PackageManager pm = context.getPackageManager();
        // FEATURE_CAMERA - 后置相机
        // FEATURE_CAMERA_FRONT - 前置相机
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)
                && !pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
            return false;
        }

        if ( Build.VERSION.SDK_INT < 21){
            return false;
        }
        return true;
    }

    public static boolean checkResourcesExist(Context context) {
        ResourceHelper resourceHelper = ResourceHelper.getInstance(context);
        if (0 == resourceHelper.getRawId("blurness")
            || 0 == resourceHelper.getRawId("eye")
            || 0 == resourceHelper.getRawId("mouth")
            || 0 == resourceHelper.getRawId("pose")
            || 0 == resourceHelper.getRawId("track")
            || 0 == resourceHelper.getRawId("tips_align_center")
            || 0 == resourceHelper.getRawId("tips_close_eye")
            || 0 == resourceHelper.getRawId("tips_keep_out_eye")
            || 0 == resourceHelper.getRawId("tips_keep_out_mouth")
            || 0 == resourceHelper.getRawId("tips_keeping")
            || 0 == resourceHelper.getRawId("tips_need_bottom")
            || 0 == resourceHelper.getRawId("tips_need_left")
            || 0 == resourceHelper.getRawId("tips_need_right")
            || 0 == resourceHelper.getRawId("tips_need_top")
            || 0 == resourceHelper.getRawId("tips_no_face")
            || 0 == resourceHelper.getRawId("tips_too_far")
            || 0 == resourceHelper.getRawId("tips_too_near")
            || 0 == resourceHelper.getRawId("tips_well_done")
        ) {
            return false;
        }
        return true;
    }

    public static boolean checkCameraPerm(Context context){
        if (Build.VERSION.SDK_INT >= 23){
            return context.checkSelfPermission(Manifest.permission.CAMERA)
                    == PackageManager.PERMISSION_GRANTED;
        }else{
            PackageManager pm = context.getPackageManager();
            return PackageManager.PERMISSION_GRANTED == pm.checkPermission(Manifest.permission.CAMERA, context.getPackageName());
        }
    }
}
